﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Benefit.Service.Sys
{
    public class ServiceOperatorAccount : Benefit.Interface.Sys.IOperatorAccount
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorAccount(Benefit.DB.DBManager db)
        {
            this.db = db;
        }
        /// <summary>
        /// 修改账户和操作员的对应关系
        /// </summary>
        /// <returns></returns>
        public int AddOperatorAccount(int operatorId,int accountId)
        {

            int oaid = 0;
            Models.Sys.OperatorAccount oa = new Models.Sys.OperatorAccount();
            oa.OperatorId = operatorId;
            var query = from t in db.OperatorAccount where t.AccountId == accountId select t;
            if (query.Count() > 0) //账户已经被配置过
            {
                oa = query.First();
                oa.OperatorId = operatorId;
                db.SaveChanges();
                oaid = oa.Id;
            }
            else //新增的账户
            {
                oa.AccountId = accountId;
                oa.OperatorId = operatorId;
                db.OperatorAccount.Add(oa);
                db.SaveChanges();
                oaid = oa.Id;
            }
            return oaid;
        }

        public List<Models.Sys.OperatorAccount> GetList()
        {
            var query = db.OperatorAccount.ToList();
            return query;
        }
        /// <summary>
        /// 在网页中配置账户和操作员关系
        /// </summary>
        /// <param name="oas"></param>
        public void ResetOperatorAccount(List<Models.Sys.OperatorAccount> oas)
        {
            foreach (Models.Sys.OperatorAccount account in oas)
            {
                var query = db.OperatorAccount.Where(a => a.AccountId == account.AccountId);
                if (query.Count() > 0)
                {
                    Models.Sys.OperatorAccount a = query.First();
                    a.OperatorId = account.OperatorId;
                }
                else
                {
                    db.OperatorAccount.Add(account);
                }
            }
            db.SaveChanges();
        }
    }
}